<template>
  <!--
  label-width:如果不设置的话，会让input独占一行

  -->

  <el-row type="flex" justify="center" align="middle" class="row1">
    <el-col :span="8" class="col1">
      <el-form :model="ruleForm" :rules="rules" class="demo-ruleForm" ref="ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
/* eslint-disable*/

// 引入axios
import axios from "axios";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin", //prop后面的值绑定
        password: "123456"
      },
      rules: {
        //只是样式的校验
        //用户名
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        //密码
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    login() {
      // console.log(this.$refs);
      //???

      this.$refs.ruleForm.validate(async valid => {
        //valid返回两个值 true和false
        if (!valid) {
          //如果校验失败，不让发送请求
          return;
        }
        //valid 为true的话,发送axios到后台进行校验
        const url = "http://localhost:8888/api/private/v1/login";
        let res = await axios.post(url, this.ruleForm);

        if (res.data.meta.status === 200) {
          //登录成功

          //登录成功后，会返回一个`token`令牌,存储在本地
          // console.log(res.data.data.token);
          localStorage.setItem("token", res.data.data.token);

          //先显示提示消息
          this.$message({
            message: res.data.meta.msg,
            type: "success",
            duration: 800
          });

          //然后跳转到home页面
          this.$router.push("/home");
        } else {
          //登录失败
          this.$message({
            message: res.data.meta.msg,
            type: "error",
            duration: 800
          });
        }
        // axios
        //   .post("http://localhost:8888/api/private/v1/login", this.ruleForm)
        //   .then(res => {
        //     console.log(res);
        //     if (res.data.meta.status === 200) {
        //       //登录成功

        //       //登录成功后，会返回一个`token`令牌,存储在本地
        //       // console.log(res.data.data.token);
        //       localStorage.setItem("token", res.data.data.token);

        //       //先显示提示消息
        //       this.$message({
        //         message: res.data.meta.msg,
        //         type: "success",
        //         duration: 800
        //       });

        //       //然后跳转到home页面
        //       this.$router.push("/home");
        //     } else {
        //       //登录失败
        //       this.$message({
        //         message: res.data.meta.msg,
        //         type: "error",
        //         duration: 800
        //       });
        //     }
        //   });
      });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    }
  }
};
</script>

<style scoped>
/* 父盒子row也需要设置成100% */
.row1 {
  height: 100%;
  background-color: #2d434c;
}

/* form表单的样式 */
.col1 {
  background-color: #fff;
  border-radius: 15px;
  padding: 25px 40px;
}
</style>
